// ScriptEditor.cpp : implementation file
//

#include "stdafx.h"
#include "afxdialogex.h"

#include "DroidArena.h"
#include "ScriptEditorDlg.h"


// CScriptEditorDlg dialog

IMPLEMENT_DYNAMIC(CScriptEditorDlg, CDialogEx)

CScriptEditorDlg::CScriptEditorDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CScriptEditorDlg::IDD, pParent)
{

}

CScriptEditorDlg::~CScriptEditorDlg()
{
}

BOOL CScriptEditorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	int i = m_ErrorListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	i = m_ErrorListCtrl.InsertColumn(0,_T("Error"),LVCFMT_LEFT,175);
	i = m_ErrorListCtrl.InsertColumn(1,_T("File"), LVCFMT_LEFT,100);
	i = m_ErrorListCtrl.InsertColumn(2,_T("Line"), LVCFMT_LEFT,50);
	i = m_ErrorListCtrl.GetHeaderCtrl()->GetItemCount();

	i = m_ErrorListCtrl.InsertItem(0,_T("item1"));
	i = m_ErrorListCtrl.SetItemText(0,1,_T("item2"));
	i = m_ErrorListCtrl.SetItemText(0,2,_T("2500"));

	m_ErrorListCtrl.InsertItem(1,_T("item12"));
	m_ErrorListCtrl.SetItemText(1,1,_T("item3"));
	m_ErrorListCtrl.SetItemText(1,2,_T("item5"));

	m_Project = NULL;
	m_NewProject = NULL;
	m_CurrentShip.Empty();
	m_CurrentFile.Empty();

	return TRUE;
}

void CScriptEditorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RICHEDIT21, m_SrcInput);
	DDX_Control(pDX, IDC_ERROR_LIST, m_ErrorListCtrl);
	DDX_Control(pDX, IDC_NEWFILE, m_NewButton);
	DDX_Control(pDX, IDC_SELECTSHIP, m_ShipSelect);
	DDX_Control(pDX, IDC_SELECTFILE, m_SelectFile);
}


BEGIN_MESSAGE_MAP(CScriptEditorDlg, CDialogEx)
	ON_BN_CLICKED(IDC_NEWFILE, &CScriptEditorDlg::OnBnClickedNewfile)
	ON_BN_CLICKED(IDC_OPENPROJECT, &CScriptEditorDlg::OnBnClickedOpenproject)
	ON_BN_CLICKED(IDSAVE, &CScriptEditorDlg::OnBnClickedSave)
	ON_BN_CLICKED(IDCANCEL2, &CScriptEditorDlg::OnBnClickedCancel2)
END_MESSAGE_MAP()


// CScriptEditorDlg message handlers

void CScriptEditorDlg::OnBnClickedNewfile()
{
	// TODO: Add your control notification handler code here
	CString wText;
	m_NewButton.GetWindowTextW(wText);
	if(wText == "New Project")
	{
		if(m_Project != NULL)
		{
			delete m_Project;
			m_Project = NULL;
		}

		m_Project = new CProject();
		m_NewProject = new CNewProjectDlg(m_Project);

		if(m_NewProject != NULL)
		{
			m_NewProject->DoModal();
			delete m_NewProject;
			m_NewProject = NULL;
		}

		m_NewButton.SetWindowTextW(_T("New File"));
	}
	else
	{
		CFileDialog fileDlg( TRUE, NULL, _T("newfile")+_T(FILE_EXTENSION), OFN_HIDEREADONLY,
				_T("All Files (*")+FILE_EXTENSION+_T(")|*")+FILE_EXTENSION+_T("||"), this);
		fileDlg.m_ofn.lpstrTitle = _T("Select new file location");

		CStdioFile file;
		if(file.Open(fileDlg.GetPathName(), CFile::modeCreate|CFile::modeWrite))
		{
			m_CurrentFile.Format(_T("%s"),fileDlg.GetPathName());
		}
		else
		{
			m_CurrentFile.Empty();
		}
	}
}


void CScriptEditorDlg::OnBnClickedOpenproject()
{
	// TODO: Add your control notification handler code here
	CFileDialog fileDlg( TRUE, NULL, _T("newfile.prj"), OFN_HIDEREADONLY, _T("All Files (*.prj)|*.prj||"), this);
	fileDlg.m_ofn.lpstrTitle = _T("Select project to open");

	if(fileDlg.DoModal() != IDOK)
	{
		return;
	}

	if(m_Project != NULL)
	{
		delete m_Project;
		m_Project = NULL;
	}

	m_Project = new CProject(fileDlg.GetPathName());
	m_Project->OpenProject();
}


void CScriptEditorDlg::OnBnClickedSave()
{
	// TODO: Add your control notification handler code here
	CString src;
	m_SrcInput.GetWindowTextW(src);
	CStdioFile file;

	if(file.Open(_T("test.txt"), CFile::modeCreate|CFile::modeWrite))
	{
		file.WriteString(src);
		file.WriteString(_T("\n"));
		file.Close();
	}
}


void CScriptEditorDlg::OnBnClickedCancel2()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}
